﻿@using CarCareTracker.Helper
@inject IConfigHelper config
@inject ITranslationHelper translator
@model SupplyRecordInput
@{
    var isNew = Model.Id == 0;
    var userConfig = config.GetUserConfig(User);
    var userLanguage = userConfig.UserLanguage;
}
<div class="modal-header">
    <h5 class="modal-title">@(isNew ? translator.Translate(userLanguage, "Add New Supply Record") : translator.Translate(userLanguage, "Edit Supply Record"))<small style="display:none; @(isNew ? "" : "cursor:pointer;")" class="cached-banner ms-2 text-warning" onclick='@(isNew ? "" : $"showEditSupplyRecordModal({Model.Id}, true)" )'>@translator.Translate(userLanguage, "Unsaved Changes")</small></h5>
    <button type="button" class="btn-close" onclick="hideAddSupplyRecordModal()" aria-label="Close"></button>
</div>
<div class="modal-body" onkeydown="handleEnter(this)">
    <form>
        <div class="form-group">
            <div class="row">
                <div class="col-md-6 col-12">
                    <input type="text" id="workAroundInput" style="height:0px; width:0px; display:none;">
                    <label for="supplyRecordDate">@translator.Translate(userLanguage,"Date")</label>
                    <div class="input-group">
                        <input type="text" id="supplyRecordDate" class="form-control" placeholder="@translator.Translate(userLanguage,"Date purchased")" value="@Model.Date">
                        <span class="input-group-text"><i class="bi bi-calendar-event"></i></span>
                    </div>
                    <label for="supplyRecordPartNumber">@translator.Translate(userLanguage,"Part Number")</label>
                    <input type="text" id="supplyRecordPartNumber" class="form-control" placeholder="@translator.Translate(userLanguage,"Part #/Model #/SKU #")" value="@(isNew ? "" : Model.PartNumber)">
                    <label for="supplyRecordDescription">@translator.Translate(userLanguage,"Description")</label>
                    <input type="text" id="supplyRecordDescription" class="form-control" placeholder="@translator.Translate(userLanguage,"Description of the Part/Supplies")" value="@Model.Description">
                    <label for="supplyRecordSupplier">@translator.Translate(userLanguage,"Supplier/Vendor")</label>
                    <input type="text" id="supplyRecordSupplier" class="form-control" placeholder="@translator.Translate(userLanguage,"Part Supplier")" value="@Model.PartSupplier">
                    <div class="row">
                        <div class="col-md-6 col-12">
                            <label for="supplyRecordQuantity">@translator.Translate(userLanguage,"Quantity")</label>
                            <div class="input-group">
                                <input type="text" inputmode="decimal" onkeydown="interceptDecimalKeys(event)" onkeyup="fixDecimalInput(this, 2)" id="supplyRecordQuantity" class="form-control" placeholder="@translator.Translate(userLanguage,"Quantity")" value="@(isNew ? "" : Model.Quantity.ToString("N2"))">
                                <div class="input-group-text">
                                    <button type="button" class="btn btn-sm zero-y-padding btn-primary" onclick="replenishSupplies()"><i class="bi bi-plus"></i></button>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6 col-12">
                            <label for="supplyRecordCost">@translator.Translate(userLanguage,"Cost")</label>
                            <input type="text" inputmode="decimal" onkeydown="interceptDecimalKeys(event)" onkeyup="fixDecimalInput(this, 2)" id="supplyRecordCost" class="form-control" placeholder="@translator.Translate(userLanguage,"Cost")" value="@(isNew ? "" : Model.Cost)">
                        </div>
                    </div>
                    <label for="supplyRecordTag">@translator.Translate(userLanguage, "Tags(optional)")</label>
                    <select multiple class="form-select" id="supplyRecordTag">
                        @foreach (string tag in Model.Tags)
                        {
                            <!option value="@tag">@tag</!option>
                        }
                    </select>
                    @await Html.PartialAsync("_ExtraField", Model.ExtraFields)
                </div>
                <div class="col-md-6 col-12">
                    <label for="supplyRecordNotes">@translator.Translate(userLanguage,"Notes(optional)")<a class="link-underline link-underline-opacity-0" onclick="showLinks(this)"><i class="bi bi-markdown ms-2"></i></a></label>
                    <textarea id="supplyRecordNotes" class="form-control" rows="5">@Model.Notes</textarea>
                    <div>
                        @await Html.PartialAsync("_UploadedFiles", Model.Files)
                        @await Html.PartialAsync("_FileUploader", Model.Files.Any())
                    </div>
                    <div id="filesPendingUpload"></div>
                </div>
            </div>
        </div>
    </form>
</div>
<div class="modal-footer">
    @if (!isNew)
    {
        @if (Model.RequisitionHistory.Any())
        {
            <button type="button" class="btn btn-warning" onclick="toggleSupplyUsageHistory()"><i class="bi bi-clock-history"></i></button>
        }
        <button type="button" class="btn btn-danger" onclick="deleteSupplyRecord(@Model.Id)" style="margin-right:auto;">@translator.Translate(userLanguage,"Delete")</button>
    }
    <button type="button" class="btn btn-secondary" onclick="hideAddSupplyRecordModal()">@translator.Translate(userLanguage,"Cancel")</button>
    @if (isNew)
    {
        <button type="button" class="btn btn-primary" onclick="saveSupplyRecordToVehicle()">@translator.Translate(userLanguage,"Add New Supply Record")</button>
    }
    else if (!isNew)
    {
        <button type="button" class="btn btn-primary" onclick="saveSupplyRecordToVehicle(true)">@translator.Translate(userLanguage,"Edit Supply Record")</button>
    }
</div>
@await Html.PartialAsync("Supply/_SupplyRequisitionHistory", new SupplyRequisitionHistory { RequisitionHistory = Model.RequisitionHistory, CostInputId = "" })
<script>
    function getSupplyRecordModelData() {
        return { id: @Model.Id}
    }
</script>